package cn.edu.zut.controller;

import cn.edu.zut.domin.Book;
import cn.edu.zut.domin.BookLendInfo;
import cn.edu.zut.service.IBookLendInfoService;
import cn.edu.zut.service.IBookService;
import com.github.pagehelper.PageInfo;
import javafx.util.Pair;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//控制器控制页面实现逻辑操作，完成功能
@Controller
@RequestMapping("/return")//响应
public class ReturnController {
    ClassPathXmlApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");
    IBookLendInfoService bookLendInfoService=(IBookLendInfoService) ac.getBean("bookLendInfoServiceImpl");
    IBookService bookService=(IBookService)ac.getBean("bookServiceImpl");
    @RequestMapping("/findAll")//响应
    public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                @RequestParam(name = "size", required = true, defaultValue = "4") int size, String readerId,
                                @ModelAttribute("param") String param) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<BookLendInfo> bookLendInfos=new ArrayList<>();
        //如果是jsp里传进来的readerId
        if(readerId==null){
            bookLendInfos=bookLendInfoService.findBookLendInfoByReader(page,size,param);
        }
        else {//从下面的returnBook里传来的readerId
            bookLendInfos=bookLendInfoService.findBookLendInfoByReader(page,size,readerId);
        }
        BookLendInfo BL=new BookLendInfo();BL.setBookId("您没有借阅信息");
        BL.setReaderId(readerId);
        Book BB=new Book();BB.setName("您没有借阅信息");//设初值
        List<Pair<BookLendInfo, Book> > pairs = new ArrayList<>();
        for(BookLendInfo bookLendInfo:bookLendInfos){
            Book nowbook=bookService.findBookById(bookLendInfo.getBookId()).get(0);
            pairs.add(new Pair<>(bookLendInfo,nowbook));
        }
        if(pairs.size()<1){
            pairs.add(new Pair<>(BL,BB));
        }//添加信息到pairs
        PageInfo<Pair<BookLendInfo, Book>> pageInfo = new PageInfo<>(pairs);
        System.out.println(pairs.size()+"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"+readerId+param);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("Return-list");
        return mv;
    }
    @RequestMapping("/returnBook")
    public String deleteBookIndexByTypeId(String bookId, String readerId, RedirectAttributes redirectAttributes) throws Exception {
        System.out.println("//////////:"+readerId+bookId+"////////////");
        //设置重定向返回的readerId
        redirectAttributes.addFlashAttribute("param",readerId);
        List<BookLendInfo> bookLendInfos =  bookLendInfoService.getBookLendInfoByBookIdAndReaderId(4,3,bookId,readerId);
        BookLendInfo bookLendInfo = bookLendInfos.get(0);
        //如果已经归还
        if(bookLendInfo.getActualReturnDate()!=null) return "redirect:findAll";
        bookLendInfo.setActualReturnDate(new Date());
        long daysBetween=(bookLendInfo.getActualReturnDate().getTime()-bookLendInfo.getDueReturnDate().getTime()+1000000)/(60*60*24*1000);
        System.out.println("相隔 "+daysBetween+" 天");
        bookLendInfo.setFineAmount(Math.max(daysBetween*0.5,0));
        //更新借阅信息
        bookLendInfoService.updateBookLendInfo(bookLendInfo);
        Book book=bookService.findBookById(bookId).get(0);
        book.setLentAmount(book.getLentAmount()-1);
        //跟新书的信息
        bookService.updateBook(book);
        return "redirect:findAll";
    }
}
/* SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(dd));// new Date()为获取当前系统时间*/